package com.b.sts.service.impl;

import com.b.sts.entity.LogEntity;
import com.b.sts.mapper.LogMapper;
import com.b.sts.service.LogService;
import com.b.sts.service.ProductService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, LogEntity> implements LogService {
    @Resource
    LogMapper mapper;
    @Resource
    ProductService productService;

    @Override
    public boolean add(LogEntity logEntity) {
        return mapper.insert(logEntity) > 0;
    }

    @Override
    public List<LogEntity> list(LogEntity entity) {
        LambdaQueryWrapper<LogEntity> wrapper = new LambdaQueryWrapper<>();
        //        String description = entity.getDescription();
        //        if (StringUtils.hasText(description)) {
        //            wrapper.like("description", entity.getDescription());
        //        } else {
        wrapper.setEntity(entity);
        wrapper.orderByDesc(LogEntity::getCreatedAt);
        wrapper.last("limit 5");
        //        }
        List<LogEntity> list = mapper.selectList(wrapper);
        for (LogEntity logEntity : list) {
            logEntity.setProductEntity(productService.getById(logEntity.getProductId()));
        }
        return list;
    }
}
